//
//  StatusCell.swift
//  mySina
//
//  Created by mac on 15/12/21.
//  Copyright © 2015年 tz. All rights reserved.
//

import UIKit
import SnapKit
class StatusCell: UITableViewCell {
    var bottomViewTopConstraints: Constraint?
    //定义微博模型属性
    var status:Status? {
        didSet {
            //给顶部视图设置模型数据
            topView.status = status
            //卸载掉顶部约束
            bottomViewTopConstraints?.uninstall()
            if let retweetedStatus = status?.retweeted_status {
                //转发微博
                //修改约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                //设置转发微博的数据源
                retweetedView.retweetedStatus = retweetedStatus
                //显示转发微博
                retweetedView.hidden = false
            } else {
                //原创微博
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(topView.snp_bottom).constraint
                })
                //隐藏转发微博
                retweetedView.hidden = true
            }
        }
    }
    //cell入口
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        //设置cell选中的样式
        selectionStyle = .None
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //自定义cell
    private func setupUI() {
        //使用cotentView添加子视图
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        //设置约束
        //顶部视图约束
        topView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
            //设置高度
//            make.height.equalTo(50)
        }
        //底部视图约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(self)
            self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.height.equalTo(44)
        }
        //转发微博的约束
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom)
            make.right.left.equalTo(contentView)
//            make.height.equalTo(50)
        }
        //给contentView设置行高
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }
    //懒加载子视图
    //顶部视图
    private lazy var topView:StatusCellTopView = StatusCellTopView()
    //底部视图
    private lazy var bottomView:StatusCellBottomView = StatusCellBottomView()
    //转发微博
    private lazy var retweetedView:StatusRetweetedView = StatusRetweetedView()
}
